home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13732 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.8 KB  |  116 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: String assignments, please help (beginner)
  5. Date: Wed, 10 Apr 96 01:14:28 GMT
  6. Organization: none
  7. Message-ID: <829098868snz@genesis.demon.co.uk>
  8. References: <4ke05g$27q@soap.news.pipex.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4ke05g$27q@soap.news.pipex.net> tone@dial.pipex.com "Tone" writes:
  15.  
  16. >I've just been learning C for the last few weeks and have come across
  17. >a problem with assigning strings, which I will detail below.
  18. >(Sorry the posting is so long, I'm just trying to make sure you know
  19. >exactly what my problem is):
  20. >
  21. >I was originally informed that the following was valid:
  22. >
  23. >        char string1[30];
  24. >        string1 = "Test string";
  25.  
  26. No, you can't assign to an array in C.
  27.  
  28. >This gave me an error on the second line although when declaring I am
  29. >able to use:
  30. >
  31. >        char string1[30] = "Test string";
  32.  
  33. C permits array initialisations.
  34.  
  35. >I have then read up and found the following info elsewhere (quote):
  36. >
  37. >        a = "This is a string.";
  38. >        is only possible if a is a char pointer.
  39.  
  40. Not 100% true but true for nearly all practical purposes.
  41.  
  42. >so I arrived at the following:
  43. >
  44. >        char *string1[30];
  45. >        *string1 = "Test string";
  46.  
  47. Not what you wanted. Here string1 is an array of 30 pointers to char and
  48. you assigned the first array element to point to your string. You can write:
  49.  
  50.          char *string2;
  51.          string2 = "Test string";
  52.  
  53.  
  54. >This compiles okay and gives the desired result in the small program I
  55. >was writing.
  56.  
  57. It would probably work the way you used it but you are still carrying
  58. around 29 unused pointers!
  59.  
  60. >However, I have since found the following information (quote):
  61. >
  62. >        int *c;
  63. >        *c = 4;
  64. >
  65. >        can and probably will give some unexpected and unwelcome results!
  66. >        As the value of c is random, the memory-address to which 4 is
  67. >        assigned is random, too. This means that you could change basically
  68. >        any location in the memory, including machine code.
  69.  
  70. That is true but in the previous cases the assignment was to the pointer
  71. itself and not what the pointer pointed to so they don't dereference an
  72. uninitialised pointer.
  73.  
  74. >I am assuming that these "unexpected results" mentioned above will
  75. >also apply to the code I have written.
  76.  
  77. No, in your code *string1 references the first element of the array, it is
  78. the same as writing string1[0]. The C language definition makes them
  79. interchangeable - a[b] is defined by the language as (*(a + (b))). So a[0]
  80. is (*(a + (0)) i.e. *a.
  81.  
  82. >The only other way I know of assigning a string is to create a loop
  83. >(e.g. a "for" loop) and assign each character of the string
  84. >individually and manually add the '\0' at the end. This seems
  85. >extremely laborious and I'm sure it can't be the best way. (Also it
  86. >may give similar "unexpected results"?)
  87.  
  88. By assigning a string I assume you mean copying it from one array to another.
  89. The strcpy() function is provided to do just this.
  90.  
  91. #include <string.h>
  92.  
  93. ...
  94.  
  95.     char string1[30];
  96.  
  97.     strcpy(string1, "Test string");
  98.  
  99.  
  100. >My questions are as follows:
  101. >
  102. >1)   When using pointers as shown above, do I need to set the memory
  103. >location of the string variable in some way before using it (and if
  104. >so, how).
  105.  
  106. Pointers need to point to a valid object before you can dereference them.
  107. It is up to you to make sure that they do. If you haven't explicitly caused
  108. a pointer to point to something via initialisation or assignment then
  109. it doesn't point to anything valid.
  110.  
  111. -- 
  112. -----------------------------------------
  113. Lawrence Kirby | fred@genesis.demon.co.uk
  114. Wilts, England | 70734.126@compuserve.com
  115. -----------------------------------------
  116.